<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script>
function shuffleCards(cards) {  
 for (let i = cards.length - 1; i > 0; i--) {  
   const j = Math.floor(Math.random() * (i + 1));  
   [cards[i], cards[j]] = [cards[j], cards[i]];  
 }  
 return cards;  
}

    // 创建一个包含 54 张牌的牌数组  
const cards = new Array(54).fill(null);
// 为每张牌分配一个唯一的 ID  
for (let i = 0; i < cards.length; i++) {  
 cards[i] = { id: i, value: i + 1 };  
}
// 将牌数组随机洗牌  
shuffleCards(cards);
// 将牌数组分成 3 份，每份 17 张牌  
const playersCards = [  
 [],  
 [],  
 [],  
];  
for (let i = 0; i < cards.length; i++) {  
 const index = Math.floor(Math.random() * playersCards.length);  
 playersCards[index].push(cards[i]);  
}
// 从牌数组中取出 3 张牌作为底牌  
const dealerCards = [  
 cards[Math.floor(Math.random() * cards.length)],  
 cards[Math.floor(Math.random() * cards.length)],  
 cards[Math.floor(Math.random() * cards.length)],  
];
// 将剩余的牌按照顺子、刻子、对子等规则处理  
function processCards(cards) {  
 const result = [];
 // 处理顺子  
 for (let i = 0; i < cards.length - 2; i++) {  
   if (cards[i].value === cards[i + 1].value + 1 && cards[i + 1].value === cards[i + 2].value + 1) {  
     result.push([cards[i], cards[i + 1], cards[i + 2]])  
     i += 2;  
   }  
 }
 // 处理刻子  
 for (let i = 0; i < cards.length - 2; i++) {  
   if (cards[i].value === cards[i + 1].value && cards[i + 1].value === cards[i + 2].value) {  
     result.push([cards[i], cards[i + 1], cards[i + 2]])  
     i += 2;  
   }  
 }
 // 处理对子  
 for (let i = 0; i < cards.length - 1; i++) {  
   if (cards[i].value === cards[i + 1].value) {  
     result.push([cards[i], cards[i + 1]])  
     i++;  
   }  
 }
 return result;  
}
// 处理发牌后的牌  
const hands = [  
 playersCards[0],  
 playersCards[1],  
 playersCards[2],  
];  
const dealerHand = dealerCards;  
const allCards = hands.concat(dealerHand);  
const result = processCards(allCards);
console.log("发牌结果：");  
console.log("玩家 1：", playersCards[0]);  
console.log("玩家 2：", playersCards[1]);  
console.log("玩家 3：", playersCards[2]);  
console.log("底牌：", dealerCards);  
console.log("顺子/刻子/对子：", result); 
</script>
</body>
</html>